package com.xiachufang.app.generator;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyCodeGenerator {

    // 你需要修改的地方
    private static final String tableName = "user";
    private static final String description = "";



    // 整个项目固定的配置 --begin
    private static final String URL = "jdbc:mysql://localhost:3306/information_schema";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";
    private static final String SCHEMA = "foods-app";

    private static final String BASE_PATH = "D:\\直播训练营\\foods-app\\src\\main\\java\\";
    private static final String BASE_PATH_RESOURCES = "D:\\直播训练营\\foods-app\\src\\main\\resources\\";
    private static final String PACKAGE_PATH = "com\\xiachufang\\app\\";

    private static final String AUTHOR = "青哥哥";
    private static final String TIME = DateUtil.now();

    private static final String PREFIX_IGNORE = "t_";
    // 整个项目固定的配置 --end

    public static void main(String[] args) throws SQLException {
        DataSource ds = new SimpleDataSource(URL, USERNAME, PASSWORD);
        Db db = Db.use(ds);

        // 所有表格行
        List<Entity> list = db.query("select * from COLUMNS where TABLE_SCHEMA = ? and TABLE_NAME = ?", SCHEMA, tableName);
        StringBuilder fields = new StringBuilder();
        for (Entity entity : list) {
            // 首字母小写
            String columnName = entity.getStr("COLUMN_NAME");
            String columnComment = entity.getStr("COLUMN_COMMENT");
            String sqlType = entity.getStr("DATA_TYPE");
            String javaType = sqlTypeToJavaType(sqlType);
            if (!columnName.equals("id")) {
                fields.append("    @ApiModelProperty(value = \"").append(columnComment).append("\")\n");
                fields.append("    private ").append(javaType).append(" ").append(StrUtil.toCamelCase(columnName)).append(";\n\n");
            }
        }
        // 构建参数
        String lowerClassName = StrUtil.toCamelCase(tableName.replaceFirst(PREFIX_IGNORE, tableName));
        String className = StrUtil.upperFirst(lowerClassName);
        Map<String, Object> map = new HashMap<>();
        map.put("description", description);
        map.put("fields", fields);
        map.put("lowerClassName", lowerClassName);
        map.put("className", className);
        map.put("tableName", tableName);
        map.put("author", AUTHOR);
        map.put("time", TIME);

        mp(className, map);
        jpa(className, map);
    }

    private static void mp(String className, Map<String, Object> map) {
        String entity = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Entity.template"), map);
        FileUtil.writeUtf8String(entity, BASE_PATH + PACKAGE_PATH + "entity\\" + className + ".java");

        System.out.println(className + ".java生成成功");


        String mapper = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Dao.template"), map);
        FileUtil.writeUtf8String(mapper, BASE_PATH + PACKAGE_PATH + "mapper\\" + className + "Mapper.java");
        System.out.println(className + "Mapper.java生成成功");

        String mapperXml = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/MapperXml.template"), map);
        FileUtil.writeUtf8String(mapperXml, BASE_PATH_RESOURCES + "mapper\\" + className + "Mapper.xml");
        System.out.println(className + "Mapper.xml生成成功");

        String service = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Service.template"), map);
        FileUtil.writeUtf8String(service, BASE_PATH + PACKAGE_PATH + "service\\" + className + "Service.java");
        System.out.println(className + "Service.java生成成功");

        String controller = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Controller.template"), map);
        FileUtil.writeUtf8String(controller, BASE_PATH + PACKAGE_PATH + "controller\\" + className + "Controller.java");
        System.out.println(className + "Controller.java生成成功");
    }

    private static void jpa(String className, Map<String, Object> map) {
        String entity = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Entity.template"), map);
        FileUtil.writeUtf8String(entity, BASE_PATH + PACKAGE_PATH + "entity\\" + className + ".java");

        System.out.println(className + ".java生成成功");


        String mapper = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Dao.template"), map);
        FileUtil.writeUtf8String(mapper, BASE_PATH + PACKAGE_PATH + "mapper\\" + className + "Mapper.java");
        System.out.println(className + "Mapper.java生成成功");

        String mapperXml = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/MapperXml.template"), map);
        FileUtil.writeUtf8String(mapperXml, BASE_PATH_RESOURCES + "mapper\\" + className + "Mapper.xml");
        System.out.println(className + "Mapper.xml生成成功");

        String service = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Service.template"), map);
        FileUtil.writeUtf8String(service, BASE_PATH + PACKAGE_PATH + "service\\" + className + "Service.java");
        System.out.println(className + "Service.java生成成功");

        String controller = StrUtil.format(FileUtil.readUtf8String(BASE_PATH + PACKAGE_PATH + "generator/templates/Controller.template"), map);
        FileUtil.writeUtf8String(controller, BASE_PATH + PACKAGE_PATH + "controller\\" + className + "Controller.java");
        System.out.println(className + "Controller.java生成成功");
    }

    /**
     * 把sql类型转换成Java类型
     * @param sqlType
     * @return
     */
    private static String sqlTypeToJavaType(String sqlType) {
        switch (sqlType) {
            case "varchar":
                return "String";
            case "bigint":
                return "Long";
            case "int":
                return "Integer";
            case "double":
                return "Double";
            case "decimal":
                return "BigDecimal";
            case "tinyint":
                return "Boolean";
            default:
                return "";
        }
    }
}
